<template>
  <div class="app-container">
    <el-card class="box-card">

      <div class="flex items-center flex-wrap">
        <el-input v-model="listQuery.user_code" class="mr-3" placeholder="用户编号" style="width: 200px;"></el-input>
        <el-input v-model="listQuery.nickname" class="mr-3" placeholder="用户昵称" style="width: 200px;"></el-input>
        <el-select v-model="listQuery.audit_status" class="mr-3" clearable filterable placeholder="审核状态"
                   style="width: 200px;"
        >
          <el-option v-for="item in options1"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value"
          >
          </el-option>
        </el-select>
        <el-select v-model="listQuery.interview_status" class="mr-3" clearable filterable placeholder="面试状态"
                   style="width: 200px;"
        >
          <el-option v-for="item in options2"
                     :key="item.value"
                     :label="item.label"
                     :value="item.value"
          >
          </el-option>
        </el-select>
        <el-button type="primary" @click="searchSubmit">查询</el-button>
        <el-button type="primary" @click="resetSubmit">重置</el-button>
      </div>

      <el-table
        ref="multipleTable"
        v-loading="loading"
        :data="tableData"
        border
        class="mt-3"
        stripe
        style="width: 100%"
        tooltip-effect="dark"
      >
        <el-table-column label="用户编号" prop="user_code"></el-table-column>
        <el-table-column label="用户昵称" prop="nickname"></el-table-column>
        <el-table-column label="审核状态" prop="audit_status">
          <template slot-scope="{row}">
            <span v-if="row.audit_status === 0">待审核</span>
            <span v-if="row.audit_status === 1">通过</span>
            <span v-if="row.audit_status === -1">拒绝</span>
          </template>
        </el-table-column>
        <el-table-column label="申请时间" prop="apply_time"></el-table-column>
        <el-table-column label="审核时间" prop="audit_time"></el-table-column>
        <el-table-column label="面试时间" prop="interview_start_at"></el-table-column>
        <el-table-column label="面试状态" prop="interview_status">
          <template v-if="row.audit_status === 1" slot-scope="{row}">
            <span v-if="row.interview_status === -1">拒绝</span>
            <span v-if="row.interview_status === 0">待面试</span>
            <span v-if="row.interview_status === 2">未面试</span>
          </template>
        </el-table-column>
        <el-table-column label="面试次数" prop="interview_count"></el-table-column>
        <el-table-column label="审核人" prop="audit_nickname"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="{row}">
            <el-button
              v-if="row.audit_status === 0"
              icon="el-icon-edit"
              plain
              size="mini"
              type="primary"
              @click="handleAudit(row)"
            >审核
            </el-button>
            <el-button
              v-if="row.interview_status === 2"
              plain
              size="mini"
              type="success"
              @click="handleInterview(row)"
            >面试
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <cus-pagination
        v-show="listQuery.total > 0"
        :limit.sync="listQuery.perPage"
        :page.sync="listQuery.page"
        :total="listQuery.total"
        @pagination="getTableData"
      />

      <el-dialog
        :title="title"
        :visible.sync="visibleInterview"
        width="30%"
        @close="closeDialog('newHeadlines')"
      >
        <el-form ref="newHeadlines" :inline="false" :model="form" :rules="rules" label-width="120px" size="normal">
          <el-form-item label="面试场次" prop="session">
            <el-select v-model="form.session" clearable filterable placeholder="请选择场次">
              <el-option v-for="item in options3"
                         :key="item.id"
                         :label="item.name"
                         :value="item.id"
              >
              </el-option>
            </el-select>

          </el-form-item>

        </el-form>

        <span slot="footer">
          <el-button @click="visibleInterview = false">返回</el-button>
          <el-button type="primary" @click="onSubmit">提交</el-button>
        </span>
      </el-dialog>

    </el-card>
  </div>
</template>

<script>
import { applyList, streamerInterview } from '@/api/sys/anchorList'
import { InterviewList } from '@/api/anchorManagement/anchorInterviewSetting'

export default {
  name: 'qualification',
  components: {},
  data() {
    return {
      title: '',
      visibleInterview: false,
      form: {
        session: ''
      },
      options1: [
        { label: '待审核', value: 0 },
        { label: '通过', value: 1 },
        { label: '拒绝', value: -1 }
      ],
      options2: [
        { label: '待面试', value: 0 },
        { label: '未面试', value: 2 },
        // { label: "通过", value: 1 },
        { label: '拒绝', value: -1 }
      ],
      options3: [],
      tableData: [],
      listQuery: {
        page: 1,
        perPage: 10,
        total: 1
      },
      rules: {
        session: [{ required: true, message: '请选择面试场次', trigger: 'blur' }]
      },
      loading: false,
      apply_id: undefined
    }
  },
  created() {
    this.getTableData()
    this.getInterviewSession()
  },
  methods: {
    handleInterview(row) {
      this.title = '面试邀请'
      this.visibleInterview = true
      this.apply_id = row.id
    },
    // 面试
    onSubmit() {
      this.$refs['newHeadlines'].validate(valid => {
        if (valid) {
          const data = {
            apply_id: this.apply_id,
            interview_room_id: this.form.session
          }
          this.StreamerInterview(data)
        }
      })
    },
    StreamerInterview(data) {
      streamerInterview(data).then(res => {
        if (res.code === 1) {
          this.$message({
            type: 'success',
            message: '操作成功'
          })
        }
        this.visibleInterview = false
        this.getTableData()
      })
    },

    // 网络请求
    /**
     * getTableData
     * @desc 获取表格数据
     */
    async getTableData() {

      this.loading = true
      for (let key in this.listQuery) {
        this.listQuery[key] = this.listQuery[key] === '' ? undefined : this.listQuery[key]
      }
      const detail = await applyList(this.listQuery)
      if (detail.code === 200) {
        this.tableData = detail.data.data
        this.listQuery.page = detail.data.current_page
        this.listQuery.total = detail.data.total
        this.listQuery.perPage = Number(detail.data.per_page)
        this.loading = false
      }
    },
    async getInterviewSession() {
      const detail = await InterviewList({
        perPage: 9000,
        is_now: 1
      })
      if (detail.code === 200) {
        this.options3 = detail.data.data
      }
    },
    closeDialog(form) {
      if (this.$refs[form]) {
        this.$refs[form].clearValidate() // 清除整个表单的校验
        this.$refs[form].resetFields() // 清除整个表单的校验
      }
    },
    searchSubmit() {
      this.listQuery.page = 1
      this.getTableData()
    },
    resetSubmit() {
      this.listQuery = {
        page: 1,
        perPage: 10
      }
      this.getTableData()
    },
    handleAudit(row) {
      this.$router.push({
        path: 'detailsPage',
        query: {
          id: row.id
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
